from __future__ import with_statement
def start(args):
    import os
    from string import Template
    
    path, file = os.path.split(__file__)
    
    js_file = os.path.join(path, 'main.coffee')
    ide_file = os.path.join(path, 'ide.html')
    
    loader = args['loader']
    rpc = loader.get_component('rpc')
    router = loader.get_component('server.WSGI_Server').get_router()

    if not os.path.exists(os.path.expanduser('~/projects')):
        os.mkdir(os.path.expanduser('~/projects'))

    @rpc.function('ide.open_project')
    def open_project(name):
        return ['file1', 'file2']
        
    @rpc.function('ide.create_project')
    def create_project(name, lang, proj):
        return None
    
    @router.route('/ide/main')
    def main(header, request, response_headers):
        with open(ide_file, 'r') as f:
            html = f.read()
        with open(js_file, 'r') as f:
            js = f.read()
        return Template(html).substitute(js=js)
    
def stop(args):
    loader = args['loader']
    router = loader.get_component('server.WSGI_Server').get_router()
    router.delete_route('/ide/main')
    router.delete_route('/ide/codemirror/*')
